package org.achao.utils;

import com.alibaba.fastjson.JSONObject;
import org.achao.common.beans.req.MyHttpClientReq;
import org.achao.common.enums.AHttpClientMethodEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author achao
 * @description http-client 工具类
 * @date 2023/9/24 15:42
 */
public class HttpClientUtil {

    /**
     * apache httpclient
     *
     * @param req
     */
    public static String apacheClient(MyHttpClientReq req) {
        String url = req.getUrl();
        String param = req.getParam();
        AHttpClientMethodEnum methodEnum = req.getMethodEnum();
        Map<String, String> header = req.getHeader();

        CloseableHttpResponse resp = null;
        if (methodEnum != null && !StringUtils.isAnyEmpty(url, methodEnum.getMethod())) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            String method = methodEnum.getMethod();
            try {
                if (AHttpClientMethodEnum.GET.getMethod().equals(method)) {
                    resp = apacheClientByGet(httpClient, url, header, param);
                }
                if (AHttpClientMethodEnum.POST.getMethod().equals(method)) {
                    resp = apacheClientByPost(httpClient, url, header, param);
                }
                if (AHttpClientMethodEnum.DEL.getMethod().equals(method)) {
                    resp = apacheClientByDel(httpClient, url);
                }
                if (AHttpClientMethodEnum.PUT.getMethod().equals(method)) {
                    resp = apacheClientByPut(httpClient, url);
                }

                if (resp != null) {
                    int statusCode = resp.getStatusLine().getStatusCode();
                    if (200 == statusCode) {
                        HttpEntity entity = resp.getEntity();
                        return EntityUtils.toString(entity);
                    }
                    // 关闭资源
                    resp.close();
                }
                httpClient.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return "请求失败...";
    }

    private static CloseableHttpResponse apacheClientByPut(CloseableHttpClient httpClient, String url) {
        return null;
    }

    private static CloseableHttpResponse apacheClientByDel(CloseableHttpClient httpClient, String url) {
        return null;
    }

    private static CloseableHttpResponse apacheClientByPost(CloseableHttpClient httpClient, String url, Map<String, String> header, String param) throws IOException {
        // 创建httpclient对象
        HttpPost httpPost = new HttpPost(url);
        if (StringUtils.isNotEmpty(param)) {
            StringEntity stringEntity = new StringEntity(param, StandardCharsets.UTF_8);
            stringEntity.setContentEncoding("utf-8");
            stringEntity.setContentType("application/json");

            httpPost.setEntity(stringEntity);
        }
        // 设置请求头
        if (header != null) {
            for (String s : header.keySet()) {
                httpPost.setHeader(s, header.get(s));
            }
        }
        return httpClient.execute(httpPost);
    }

    private static CloseableHttpResponse apacheClientByGet(CloseableHttpClient httpClient, String url, Map<String, String> header, String param) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        if (StringUtils.isNotEmpty(param)) {
            // 将JSONObject转换为Map
            Map<String, Object> paramMap = JSONObject.parseObject(param);
            try {
                URIBuilder builder = new URIBuilder(url);
                if (paramMap != null) {
                    for (String key : paramMap.keySet()) {
                        builder.addParameter(key, (String) paramMap.get(key));
                    }
                }
                URI uri = builder.build();
                // 创建httpclient对象
                httpGet = new HttpGet(uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 设置请求头
        if (header != null) {
            for (String s : header.keySet()) {
                httpGet.setHeader(s, header.get(s));
            }
        }
        return httpClient.execute(httpGet);
    }

}
